package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.Arrays;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/friends-of-appropriate-ages/">825.适龄的朋友</a>
 */
public class Solution825 extends BaseSolution {

    public int numFriendRequests(int[] ages) {
        Arrays.sort(ages);
        int count = 0;
        for (int i = 0; i < ages.length - 1; i++) {
            int min = ages[i], max = 121;
            if (ages[i] > 100) {
                min = 100;
            } else {
                max = (ages[i] - 7) * 2;
            }
            for (int j = i + 1; j < ages.length; j++) {
                if (ages[j] >= max) {
                    continue;
                }
                count += ages[j] == ages[i] ? 2 : (ages[j] >= min ? 1 : 0);
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Solution825 solution = new Solution825();
        System.out.println(solution.numFriendRequests(new int[]
                        {20,30,100,110,120}
                ));
    }
}